package com.youi.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.dao.UserwechatDao;
import com.youi.entity.User;
import com.youi.entity.Userwechat;
import com.youi.service.UserService;
import com.youi.service.UserwechatService;

@Service
public class UserwechatServiceImpl extends BaseServiceImpl<Userwechat> implements UserwechatService{
		
		private UserwechatDao userwechatDao;
		
		@Autowired
		private UserService userService;
		@Autowired
		public void setfaultRecordDao(UserwechatDao userwechatDao){
			this.userwechatDao=userwechatDao;
			super.setBaseDAO(userwechatDao);
		}

		@Override
		public User getWeChatLicenseUserId(String unionid) {
			// TODO Auto-generated method stub
			if (unionid!=null&&unionid.length()>0) {
				Map<String, Object> params=new HashMap<String, Object>();
				params.put(Common.UNIONID, unionid);
				params.put(Common.DELSTATUS, DelStatus.NODEL);
				List<Userwechat> userwechats=userwechatDao.getObjectList(params);
				if (userwechats!=null&&userwechats.size()>0) {
					for (Userwechat userwechat : userwechats) {
						if (userwechat.getUserId()!=null) {
							return userService.get(userwechat.getUserId());
						}
					}
				}
			}
			return null;
		}

		@Override
		public Userwechat getUserwechat(String appid, String openid, String unionid, String subscribe,String ip) {
			// TODO Auto-generated method stub
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			if (appid!=null&&appid.length()>0) {
				params.put(Common.APPID, appid);
			}
			if (openid!=null&&openid.length()>0) {
				params.put(Common.OPENID, openid);
			}
			if (unionid!=null&&unionid.length()>0) {
				params.put(Common.UNIONID, unionid);
			}
			if (subscribe!=null&&subscribe.length()>0) {
				params.put(Common.SUBSCRIBE, subscribe);
			}
			Userwechat userother=userwechatDao.getObject(params);
			/**首次授权时*/
			if (userother==null) {
				userother=new Userwechat(openid, unionid, subscribe, appid,ip);
			}
			return userother;
		}

		@Override
		public User getNewUser(String avatarUrl, String nickName, String gender,String phone) {
			// TODO Auto-generated method stub
			String userNo = UUID.randomUUID().toString().replaceAll("-","");
			return new User(nickName, avatarUrl, getGender(gender), userNo,phone);
		}
		
		//获取性别
		private Integer getGender(String gender){
			if (gender.equals("1")) {
				return 2;
			}else if (gender.equals("2")) {
				return 1;
			}
			return 0;
		}

		@Override
		public void updateAllUnion(String unionid, Long userId) {
			// TODO Auto-generated method stub
			if (unionid!=null&&unionid.length()>0) {
				Map<String, Object> params=new HashMap<String, Object>();
				params.put(Common.UNIONID, unionid);
				params.put(Common.DELSTATUS, DelStatus.NODEL);
				List<Userwechat> userwechats=userwechatDao.getObjectList(params);
				if (userwechats!=null&&userwechats.size()>0) {
					for (Userwechat userwechat : userwechats) {
						if (userwechat.getUserId()==null||!userwechat.getUserId().equals(userId)) {
							userwechat.setUserId(userId);
							userwechatDao.saveOrUpdate(userwechat);
						}
					}
				}
			}
		}
}